package com.tbit.uqbike.quartz;

import com.tbit.uqbike.constant.FestivalActivityConstant;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.webmanager.dao.core.*;
import com.tbit.uqbike.webmanager.util.LogUtil;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: chen
 * @Description: 生日赠送活动
 * @Date: 创建于 15:33 2020/6/10
 */
@Component
public class BirthGiveQuartz {

    @Autowired
    private FestivalActivityDao festivalActivityDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserToAgentDao userToAgentDao;
    @Autowired
    private GiftCardUserDao giftCardUserDao;
    @Autowired
    private CouponDao couponDao;
    @Autowired
    private CouponUserDao couponUserDao;

    /**
     * 2023-05-12 02:00:00
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void give() {
        LogUtil.info("开始定时任务，统计生日赠送活动");
        /**用户id和区域绑定哈希*/
        Map<Integer, UserToAgent> map = new HashMap<>(8);
        /**区域id和生日活动绑定哈希*/
        Map<Integer, FestivalActivity> param = new HashMap<>(8);
        /**查询所有配置了生日赠送活动的代理*/
        List<FestivalActivity> festivalActivities = festivalActivityDao.getAll(FestivalActivityConstant.TYPE_BIRTH);
        if (festivalActivities.size() > 0) {
            List<Integer> accountIds = new ArrayList<>();
            for (FestivalActivity f : festivalActivities) {
                accountIds.add(f.getAccountId());
                param.put(f.getAccountId(), f);
            }
            /**查询所有生日在今天的用户*/
            List<Integer> userIds = userDao.getByBirthDay(DateTimeUtil.getNowTimeYMD());
            /**查询区域下所有用户*/
            List<UserToAgent> userToAgents = userToAgentDao.getByAgent(accountIds);
            for (UserToAgent u : userToAgents) {
                if (userIds.contains(u.getUserId())) {
                    UserToAgent userToAgentDB = map.get(u.getUserId());
                    /**哈希map中该用户不存在，或存在的区域添加时间比现有的要晚，则覆盖*/
                    if (userToAgentDB == null || userToAgentDB.getUpdateDt().getTime() < u.getUpdateDt().getTime()) {
                        map.put(u.getUserId(), u);
                    }
                }
            }
            /**遍历map 添加赠送*/
            for (Map.Entry<Integer, UserToAgent> entry : map.entrySet()) {
                Integer userId = entry.getKey();
                UserToAgent userToAgent = entry.getValue();
                /**获取活动配置*/
                FestivalActivity festivalActivity = param.get(userToAgent.getAccountId());
                if (festivalActivity != null) {
                    /**赠送金额不为空*/
                    if (festivalActivity.getGiveMoney() > 0) {
                        GiftCardUser giftCardUser = new GiftCardUser();
                        giftCardUser.setMoney(festivalActivity.getGiveMoney());
                        giftCardUser.setAccountId(userToAgent.getAccountId());
                        giftCardUser.setUserId(userId);
                        giftCardUser.setUpdateTime(new Date());
                        giftCardUserDao.insert(giftCardUser);
                    }
                    /**赠送优惠券*/
                    if (festivalActivity.getCouponId() != null) {
                        Coupon coupon = couponDao.getByCouponId(festivalActivity.getCouponId());
                        if (coupon != null) {
                            List<CouponUser> userList = new LinkedList<>();
                            for (int i = 0; i < festivalActivity.getCouponCount(); i++) {
                                CouponUser couponUser = new CouponUser();
                                couponUser.setAccountId(userToAgent.getAccountId());
                                couponUser.setCouponId(festivalActivity.getCouponId());
                                couponUser.setUserId(userId);
                                couponUser.setIsUse(0);
                                couponUser.setIsValid(1);
                                couponUser.setRecTime(DateTimeUtil.getNowTime());
                                couponUser.setExpireTime(DateTimeUtil.DateTimeToString(DateTimeUtil.addDay(new Date(), festivalActivity.getValidDays())));
                                userList.add(couponUser);
                            }
                            couponUserDao.addBatch(userList);
                        } else {
                            LogUtil.info("优惠券不存在,代理id=" + userToAgent.getAccountId() + ",优惠券id=" + festivalActivity.getCouponId());
                        }
                    }
                }
            }
        }
    }

}
